//
//  CAKeyFrameAnimationViewController.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/7/26.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "CAKeyFrameAnimationViewController.h"

@interface CAKeyFrameAnimationViewController ()<CAAnimationDelegate>
{
    BOOL doudongFlg;
    CALayer *_carLayer;
}
@property (weak, nonatomic) IBOutlet UIImageView *toudongImgView;

@end
//抖动
#define kRadio(angle) ((angle) / 100.0 * M_PI)
static NSString * const kDouDongImgViewKey = @"kDouDongImgViewKey";
//车车
static NSString * const kCarAnimationKey = @"kCarAnimationKey";
@implementation CAKeyFrameAnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"CAKeyFrameAnimation";
    //1.抖动
    [self doudong];
    //2.车车前进
    [self checheGo];
    
    
    
    
}
//抖动
- (void)doudong{
    doudongFlg = NO;
    self.toudongImgView.userInteractionEnabled = YES;
    //正在抖动
    [self doudonging];
    //抖还是不抖动
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImgView:)];
    [self.toudongImgView addGestureRecognizer:tapGes];
}
#pragma mark --- 1.tapImgView点击图片抖动
- (void)tapImgView:(UITapGestureRecognizer *)tap{
    doudongFlg = !doudongFlg;
    if (doudongFlg) {
        [self doudongStart];
    } else {
        [self doudongStop];
    }
    
}
//正在抖动
- (void)doudonging{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    animation.values = @[@kRadio(-3),@kRadio(3),@kRadio(-3)];
    animation.repeatCount = MAXFLOAT;
    animation.delegate = self;
    [self.toudongImgView.layer addAnimation:animation forKey:kDouDongImgViewKey];
}
//恢复抖动
- (void)doudongStart{
//    self.toudongImgView.layer.timeOffset = 0;
//
//    self.toudongImgView.layer.beginTime = CACurrentMediaTime() - self.toudongImgView.layer.timeOffset;
//
//    self.toudongImgView.layer.speed = 1.0;
    [self doudonging];
}
//停止抖动
- (void)doudongStop{
//    CFTimeInterval interval = [self.toudongImgView.layer convertTime:CACurrentMediaTime() toLayer:nil];
//    self.toudongImgView.layer.timeOffset = interval;
//    self.toudongImgView.layer.speed = 0.0;
    [self.toudongImgView.layer removeAllAnimations];
    [self.toudongImgView.layer removeAnimationForKey:kDouDongImgViewKey];
}
#pragma mark --- 代理<CAAnimationDelegate>
- (void)animationDidStart:(CAAnimation *)anim{
    if ([self.toudongImgView.layer animationForKey:kDouDongImgViewKey] == anim) {
        NSLog(@"图片开始抖动。。。");
    }
    if ([_carLayer animationForKey:kCarAnimationKey] == anim) {
        NSLog(@"车车开始启动");
    }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if ([self.toudongImgView.layer animationForKey:kDouDongImgViewKey] == anim) {
        NSLog(@"图片停止抖动。。。");
    }
    if ([_carLayer animationForKey:kCarAnimationKey] == anim) {
        NSLog(@"车车停止跑动");
    }
}




/*****************  画曲线车车前进 *******************/
#pragma mark --- 车车前行
- (void)checheGo{
    //1.画曲线
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    //开始位置
    [bezierPath moveToPoint:CGPointMake(20, 300)];
    //结束位置和中间2块基地位置
    [bezierPath addCurveToPoint:CGPointMake(310, 300) controlPoint1:CGPointMake(130, 150) controlPoint2:CGPointMake(200, 400)];
    //添加到self.view.layer上
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = bezierPath.CGPath;
    shapeLayer.fillColor = nil;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.lineWidth = 2.0;
    [self.view.layer addSublayer:shapeLayer];
    //2.画车车
    CALayer *carLayer = [CALayer layer];
    _carLayer = carLayer;
    carLayer.frame = CGRectMake(20, 300, 45, 40);
    carLayer.contents = (__bridge id)[UIImage imageNamed:@"car.jpg"].CGImage;
    carLayer.anchorPoint = CGPointMake(0.5, 0.9);
    [self.view.layer addSublayer:carLayer];
    //3.开启动画
    CAKeyframeAnimation *carAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //路径
    carAnimation.path = bezierPath.CGPath;
    //每圈4秒
    carAnimation.duration = 4.0;
    //次数
    carAnimation.repeatCount = 1;
    carAnimation.rotationMode = kCAAnimationRotateAuto;
    //动画节奏
    carAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //图层是否显示执行后的动画执行后的位置以及形态
    carAnimation.removedOnCompletion = NO;
    carAnimation.fillMode = kCAFillModeForwards;
    //动画完成后是否以动画的形式回到初始值
//    carAnimation.autoreverses = YES;
    carAnimation.delegate = self;
    carAnimation.speed = 2.0f;
    [carLayer addAnimation:carAnimation forKey:kCarAnimationKey];
   
}
- (IBAction)ClickCar:(UIButton *)sender {
    [self checheGo];
}






@end
